package com.example.wangfanwangluo.wangfanboss;

import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by wangfanwangluo on 2017/11/28.
 */

public class FragmentDocuDetail extends Fragment implements View.OnClickListener {
    private DocumentDetail orderInfo; //运单详情activity
    private Typeface iconfont;
    private TextView orderdetail_fphone_icon;
    private TextView orderdetail_tphone_icon;
    private TextView orderdetail_ziti_icon;
    private String oid; //从上级界面获取oid的值，从而进入详情

    private TextView fragment_order_detail_ordersn;
    private TextView fragment_order_detail_orderstate;
    private TextView fragment_order_detail_fplace;
    private TextView fragment_order_detail_tplace;
    private TextView fragment_order_detail_timename;
    private TextView fragment_order_detail_company;
    private TextView fod_from_name;
    private TextView fod_from_phone;
    private TextView fod_from_address;
    private TextView fod_to_name;
    private TextView fod_to_phone;
    private TextView fod_to_address;

    private TextView fod_goods_name1;
    private TextView fod_goods_num1;
    private TextView fod_goods_weight1;
    private TextView fod_goods_package1;
    private TextView fod_goods_volume1;

    private TextView fod_goods_name2;
    private TextView fod_goods_num2;
    private TextView fod_goods_weight2;
    private TextView fod_goods_package2;
    private TextView fod_goods_volume2;

    private TextView fod_goods_name3;
    private TextView fod_goods_num3;
    private TextView fod_goods_weight3;
    private TextView fod_goods_package3;
    private TextView fod_goods_volume3;

    private TextView fod_receiptmethod;
    private TextView fod_collectionstate;
    private TextView fod_collection;
    private TextView fod_insurancestate;
    private TextView fod_goods_value;
    private TextView fod_insurance_cost;

    private TextView fod_fee;
    private TextView fod_cash;
    private TextView fod_othname_fee;
    private TextView fod_receipt_num;
    private TextView fod_receipt_fee;
    private TextView fod_to_pay;

    private TextView fod_remark;

    private LinearLayout fod_from_block;
    private LinearLayout fod_to_block;

    private String from_mobile;
    private String to_mobile;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //让详情fragment与详情xml布局文件联系起来
        return inflater.inflate(R.layout.fragment_docudetail, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(); //初始化ui
    }

    private void initView(){

        orderInfo = (DocumentDetail) getActivity(); //获取主activity
        //
        Bundle bundle = orderInfo.getIntent().getExtras();
        oid = bundle.getString("oid"); //从intent中获取oid的值，用于请求数据

        iconfont = Typeface.createFromAsset(orderInfo.getAssets(), "iconfont/iconfont.ttf");
        orderdetail_fphone_icon = (TextView) orderInfo.findViewById(R.id.orderdetail_fphone_icon);
        orderdetail_fphone_icon.setTypeface(iconfont);
        orderdetail_tphone_icon = (TextView) orderInfo.findViewById(R.id.orderdetail_tphone_icon);
        orderdetail_tphone_icon.setTypeface(iconfont);
        orderdetail_ziti_icon = (TextView) orderInfo.findViewById(R.id.orderdetail_ziti_icon);
        orderdetail_ziti_icon.setTypeface(iconfont);

        fragment_order_detail_ordersn = (TextView) orderInfo.findViewById(R.id.fragment_order_detail_ordersn);
        fragment_order_detail_orderstate = (TextView) orderInfo.findViewById(R.id.fragment_order_detail_orderstate);
        fragment_order_detail_fplace = (TextView) orderInfo.findViewById(R.id.fragment_order_detail_fplace);
        fragment_order_detail_tplace = (TextView) orderInfo.findViewById(R.id.fragment_order_detail_tplace);
        fragment_order_detail_timename = (TextView) orderInfo.findViewById(R.id.fragment_order_detail_timename);
        fragment_order_detail_company = (TextView) orderInfo.findViewById(R.id.fragment_order_detail_company);
        fod_from_name = (TextView) orderInfo.findViewById(R.id.fod_from_name);
        fod_from_phone = (TextView) orderInfo.findViewById(R.id.fod_from_phone);
        fod_from_address = (TextView) orderInfo.findViewById(R.id.fod_from_address);
        fod_to_name = (TextView) orderInfo.findViewById(R.id.fod_to_name);
        fod_to_phone = (TextView) orderInfo.findViewById(R.id.fod_to_phone);
        fod_to_address = (TextView) orderInfo.findViewById(R.id.fod_to_address);

        fod_goods_name1 = (TextView) orderInfo.findViewById(R.id.fod_goods_name1);
        fod_goods_num1 = (TextView) orderInfo.findViewById(R.id.fod_goods_num1);
        fod_goods_weight1 = (TextView) orderInfo.findViewById(R.id.fod_goods_weight1);
        fod_goods_package1 = (TextView) orderInfo.findViewById(R.id.fod_goods_package1);
        fod_goods_volume1 = (TextView) orderInfo.findViewById(R.id.fod_goods_volume1);

        fod_goods_name2 = (TextView) orderInfo.findViewById(R.id.fod_goods_name2);
        fod_goods_num2 = (TextView) orderInfo.findViewById(R.id.fod_goods_num2);
        fod_goods_weight2 = (TextView) orderInfo.findViewById(R.id.fod_goods_weight2);
        fod_goods_package2 = (TextView) orderInfo.findViewById(R.id.fod_goods_package2);
        fod_goods_volume2 = (TextView) orderInfo.findViewById(R.id.fod_goods_volume2);

        fod_goods_name3 = (TextView) orderInfo.findViewById(R.id.fod_goods_name3);
        fod_goods_num3 = (TextView) orderInfo.findViewById(R.id.fod_goods_num3);
        fod_goods_weight3 = (TextView) orderInfo.findViewById(R.id.fod_goods_weight3);
        fod_goods_package3 = (TextView) orderInfo.findViewById(R.id.fod_goods_package3);
        fod_goods_volume3 = (TextView) orderInfo.findViewById(R.id.fod_goods_volume3);

        fod_receiptmethod = (TextView) orderInfo.findViewById(R.id.fod_receiptmethod);
        fod_collectionstate = (TextView) orderInfo.findViewById(R.id.fod_collectionstate);
        fod_collection = (TextView) orderInfo.findViewById(R.id.fod_collection);
        fod_insurancestate = (TextView) orderInfo.findViewById(R.id.fod_insurancestate);
        fod_goods_value = (TextView) orderInfo.findViewById(R.id.fod_goods_value);
        fod_insurance_cost = (TextView) orderInfo.findViewById(R.id.fod_insurance_cost);

        fod_fee = (TextView) orderInfo.findViewById(R.id.fod_fee);
        fod_cash = (TextView) orderInfo.findViewById(R.id.fod_cash);
        fod_othname_fee = (TextView) orderInfo.findViewById(R.id.fod_othname_fee);
        fod_receipt_num = (TextView) orderInfo.findViewById(R.id.fod_receipt_num);
        fod_receipt_fee = (TextView) orderInfo.findViewById(R.id.fod_receipt_fee);
        fod_to_pay = (TextView) orderInfo.findViewById(R.id.fod_to_pay);

        fod_remark = (TextView) orderInfo.findViewById(R.id.fod_remark);

        fod_from_block = (LinearLayout) orderInfo.findViewById(R.id.fod_from_block);
        fod_to_block = (LinearLayout) orderInfo.findViewById(R.id.fod_to_block);

        //拨打电话事件
        fod_from_block.setOnClickListener(this);
        fod_to_block.setOnClickListener(this);
//        getOrderDetail(); //请求数据
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fod_from_block://拨打发货人电话
                dailPhone(from_mobile);
                break;
            case R.id.fod_to_block://拨打收货人电话
                dailPhone(to_mobile);
                break;
            default:
                break;
        }
    }

    /*
    * 拨打电话
    * @param phone 需要呼叫的手机号
    * @return void
    * */
    private void dailPhone(String phone){
        if (!TextUtils.isEmpty(phone)){
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + phone));
            startActivity(intent);
        }
    }

//    private void getOrderDetail(){
//        RequestBody requestBody = new FormBody.Builder()
//                .add("token", functions.getToken(orderInfo))
//                .add("oid", oid)
//                .build();
//        String url = orderInfo.getResources().getString(R.string.url_prefix) + "Order/orderInfo";
//        HttpUitl.sendOkHttpRequest(url, requestBody, new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Toast.makeText(orderInfo, "网络请求失败，请检查您的网络",
//                        Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String responseData = response.body().string();
//                try {
//                    JSONObject jsonObject = new JSONObject(responseData);
//                    int code = jsonObject.getInt("code");
//                    String info = jsonObject.getString("info");
//                    switch (code){
//                        case 0://成功
//                            String data = jsonObject.getString("data");
//                            JSONObject obj = new JSONObject(data);
//                            final String order_id = obj.getString("order_id");
//                            final String orderstate = "[" + obj.getString("orderstate") + "]";
//                            final String start_place = obj.getString("start_place");
//                            final String end_place = obj.getString("end_place");
//                            final String time_name = obj.getString("create_order_name") + " " + obj.getString("created_time");
//                            final String company_name = obj.getString("company_name");
//                            final String from_person = "发货人：" + obj.getString("from_person");
//                            from_mobile = obj.getString("from_mobile");
//                            final String from_address = "地址：" + obj.getString("from_address");
//                            final String to_person = "收货人：" + obj.getString("to_person");
//                            to_mobile = obj.getString("to_mobile");
//                            final String to_address = "地址：" + obj.getString("to_address");
//
//                            final String goods_name1 = obj.getString("goods_name1");
//                            final String goods_weight1 = obj.getString("goods_weight1");
//                            final String goods_volume1 = obj.getString("goods_volume1");
//                            final String goods_num1 = obj.getString("goods_num1");
//                            final String goods_package1 = obj.getString("goods_package1");
//
//                            final String goods_name2 = obj.getString("goods_name2");
//                            final String goods_weight2 = obj.getString("goods_weight2");
//                            final String goods_volume2 = obj.getString("goods_volume2");
//                            final String goods_num2 = obj.getString("goods_num2");
//                            final String goods_package2 = obj.getString("goods_package2");
//
//                            final String goods_name3 = obj.getString("goods_name3");
//                            final String goods_weight3 = obj.getString("goods_weight3");
//                            final String goods_volume3 = obj.getString("goods_volume3");
//                            final String goods_num3 = obj.getString("goods_num3");
//                            final String goods_package3 = obj.getString("goods_package3");
//
//                            final String receiptmethod = obj.getString("receiptmethod");
//                            final String collectionstate = "代收货款：" + obj.getString("collectionstate");
//                            final String collection = "代收金额：" + handleUnit(obj.getString("collection"), 4);
//                            final String insurancestate = "物流保：" + obj.getString("insurancestate");
//                            final String goods_value = "货物价值：" + handleUnit(obj.getString("goods_value"), 4);
//                            final String insurance_cost = "保险金额：" + handleUnit(obj.getString("insurance_cost"), 4);
//
//                            final String fee = "运费：" + handleUnit(obj.getString("fee"), 4);
//                            final String cash = "现付：" + handleUnit(obj.getString("cash"), 4);
//                            final String othname_fee = obj.getString("othname") + "：" + handleUnit(obj.getString("oth_fee"), 4);
//                            final String receipt_num = "回单：" + handleUnit(obj.getString("receipt_num"), 5);
//                            final String receipt_fee = "回单付：" + handleUnit(obj.getString("receipt_fee"), 4);
//                            final String to_pay = "到付：" + handleUnit(obj.getString("to_pay"), 4);
//
//                            String tmp_remark = obj.getString("remark");
//                            if (TextUtils.isEmpty(tmp_remark)){
//                                tmp_remark = "暂无备注";
//                            }
//                            final String remark = tmp_remark;
//
//                            orderInfo.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    fragment_order_detail_ordersn.setText(order_id);
//                                    fragment_order_detail_orderstate.setText(orderstate);
//                                    fragment_order_detail_fplace.setText(start_place);
//                                    fragment_order_detail_tplace.setText(end_place);
//                                    fragment_order_detail_timename.setText(time_name);
//                                    fragment_order_detail_company.setText(company_name);
//                                    fod_from_name.setText(from_person);
//                                    fod_from_phone.setText(from_mobile);
//                                    fod_from_address.setText(from_address);
//                                    fod_to_name.setText(to_person);
//                                    fod_to_phone.setText(to_mobile);
//                                    fod_to_address.setText(to_address);
//
//                                    handleGoodsInfo(goods_name1,goods_num1,goods_weight1,goods_package1,goods_volume1,
//                                            goods_name2,goods_num2,goods_weight2,goods_package2,goods_volume2,
//                                            goods_name3,goods_num3,goods_weight3,goods_package3,goods_volume3);
//
//                                    fod_receiptmethod.setText(receiptmethod);
//                                    fod_collectionstate.setText(collectionstate);
//                                    fod_collection.setText(collection);
//                                    fod_insurancestate.setText(insurancestate);
//                                    fod_goods_value.setText(goods_value);
//                                    fod_insurance_cost.setText(insurance_cost);
//
//                                    fod_fee.setText(fee);
//                                    fod_cash.setText(cash);
//                                    fod_othname_fee.setText(othname_fee);
//                                    fod_receipt_num.setText(receipt_num);
//                                    fod_receipt_fee.setText(receipt_fee);
//                                    fod_to_pay.setText(to_pay);
//                                    fod_remark.setText(remark);
//                                }
//                            });
//                            break;
//                        case 1://参数错误
//                            Toast.makeText(orderInfo, info, Toast.LENGTH_SHORT).show();
//                            break;
//                        case 2://token失效
//                            Toast.makeText(orderInfo, info, Toast.LENGTH_SHORT).show();
//                            goLogin();
//                            break;
//                        default:
//                            break;
//                    }
//                } catch (Exception e){
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

    /*
    * 处理货物信息模块的显示
    * @param 按照“货物名称”，“货物数量”，“货物重量”，“货物包装”，“货物体积”依次传递
    * @return void
    * */
//    private void handleGoodsInfo(String... str){
//        String goodsName = orderInfo.getResources().getString(R.string.goods_name);
//        String goodsNum = orderInfo.getResources().getString(R.string.goods_num);
//        String goodsWeight = orderInfo.getResources().getString(R.string.goods_weight);
//        String goodsPackage = orderInfo.getResources().getString(R.string.goods_package);
//        String goodsVolume = orderInfo.getResources().getString(R.string.goods_volume);
//
//        fod_goods_name1.setText(String.format(goodsName, str[0]));
//        fod_goods_num1.setText(String.format(goodsNum, handleUnit(str[1], 3)));
//        fod_goods_weight1.setText(String.format(goodsWeight, handleUnit(str[2], 1)));
//        fod_goods_package1.setText(String.format(goodsPackage, str[3]));
//        fod_goods_volume1.setText(String.format(goodsVolume, handleUnit(str[4], 2)));
//
//        fod_goods_name2.setText(String.format(goodsName, str[5]));
//        fod_goods_num2.setText(String.format(goodsNum, handleUnit(str[6], 3)));
//        fod_goods_weight2.setText(String.format(goodsWeight, handleUnit(str[7], 1)));
//        fod_goods_package2.setText(String.format(goodsPackage, str[8]));
//        fod_goods_volume2.setText(String.format(goodsVolume, handleUnit(str[9], 2)));
//
//        fod_goods_name3.setText(String.format(goodsName, str[10]));
//        fod_goods_num3.setText(String.format(goodsNum, handleUnit(str[11], 3)));
//        fod_goods_weight3.setText(String.format(goodsWeight, handleUnit(str[12], 1)));
//        fod_goods_package3.setText(String.format(goodsPackage, str[13]));
//        fod_goods_volume3.setText(String.format(goodsVolume, handleUnit(str[14], 2)));
//    }
//
//    /*
//    * 处理货物信息带单位的字符串
//    * @param str: 需要处理的字符串
//    * @param type: 类型，分为1:“吨”、2:“方”、3:“件”、4：“元”\5：“份”
//    * @return String
//    * */
//    private String handleUnit(String str, int type){
//        if (!TextUtils.isEmpty(str)){
//            switch (type){
//                case 1:
//                    str += "吨";
//                    break;
//                case 2:
//                    str += "方";
//                    break;
//                case 3:
//                    str += "件";
//                    break;
//                case 4:
//                    str += "元";
//                    break;
//                case 5:
//                    str += "份";
//                    break;
//                default:
//                    break;
//            }
//        }
//        return str;
//    }

    //重新登录
    private void goLogin(){
        Intent intent = new Intent(orderInfo, Login.class);
        startActivity(intent);
        orderInfo.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        orderInfo.finish();
    }

}
